package com.chencong.http;

import com.alibaba.fastjson.JSONObject;
import com.chencong.entity.User;
import com.chencong.request.Request;
import com.chencong.service.BillService;
import com.chencong.service.PageService;
import com.chencong.service.ProviderService;
import com.chencong.service.UserService;
import com.chencong.session.MySession;
import com.chencong.util.Constant;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author chencong
 * @date 2020/12/14 上午9:25
 */
public class HttpServer {

    public static void main(String[] args) {
        System.out.println("----------服务端启动成功------------");
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(Constant.PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (true) {
            try (
                    Socket socket = serverSocket.accept();
                    //用来读取前端发来的请求和数据
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    //用来向前端页面输出数据的
                    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            ) {
                Request request = Request.parse(br);
                String url = request.getUrl();
                if (url.startsWith("/server")) {
                    System.out.println("业务请求:" + request.getUrl() + "\tdata:" + request.getData());
                    //对未登录的业务请求进行拦截
                    if(!url.equals("/server/user/getInfo")&&!url.equals("/server/user/quit")){
                        if(MySession.onlineUser==null&&!url.equals("/server/user/login")){
                            PageService ps = new PageService(dos);
                            ps.sendData("请先登录",false);
                            continue;
                        }
                    }
                    //如果是以server开始的 就需要处理数据
                    //处理数据的时候需要分发处理,不仅仅只有一个请求
                    dispatchRequest(dos, request);
                } else {
                    System.out.println("页面请求:" + url);
                    SendStaticFile(dos, request);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    public static void SendStaticFile(DataOutputStream dos, Request request) {
        PageService ps = new PageService(dos);
        try {
            //获取前端传来的页面请求
            String url = request.getUrl();
            if ("/".equals(url) || "".equals(url)) {
                url = "/login.html";
            }
            ps.toPage(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void dispatchRequest(DataOutputStream dos, Request request) throws Exception {
        UserService us = new UserService(dos, request);
        ProviderService prs = new ProviderService(dos, request);
        BillService bs = new BillService(dos, request);
        String url = request.getUrl();
        String data = request.getData();

        switch (url) {
            //用户登录
            case "/server/user/login":
                us.login(data);
                break;
            //获取在线用户
            case "/server/user/getInfo":
                us.getOnlineUser();
                break;
            //查询用户列表
            case "/server/user/list":
                us.getUsersList();
                break;
            //退出登录
            case "/server/user/quit":
                us.quit();
                break;
            //添加用户
            case "/server/user/add":
                us.addUser(data);
                break;
            //用户信息查询
            case "/server/user/get":
                us.getUser(data);
                break;
            //用户信息修改
            case "/server/user/update":
                us.updateUser(data);
                break;
            //用户删除
            case "/server/user/delete":
                us.deleteUser(data);
                break;
            //供应商列表查询
            case "/server/provider/list":
                prs.getProvidersList();
                break;
            //供应商添加
            case "/server/provider/add":
                prs.addProvider(data);
                break;
            //供应商信息查询
            case "/server/provider/get":
                prs.getProvider(data);
                break;
            //供应商信息修改
            case "/server/provider/update":
                prs.updateProvider(data);
                break;
            //删除供应商
            case "/server/provider/delete":
                prs.deleteProvider(data);
                break;
            //获取账单列表
            case "/server/bill/list":
                bs.getBillList();
                break;
            //添加账单
            case "/server/bill/add":
                bs.addBill(data);
                break;
            //账单信息查询
            case "/server/bill/get":
                bs.getBill(data);
                break;
            //账单信息修改
            case "/server/bill/update":
                bs.updateBill(data);
                break;
            //删除账单
            case "/server/bill/delete":
                bs.deleteBill(data);
                break;
            //前端发送了不存在的请求,直接跳到404页面
            default:
                System.out.println("不存在的请求" + url);
                SendStaticFile(dos, request);
                break;
        }
    }
}
